import unittest
from dictionary import Dictionary, DictionaryWord, DictionaryInfo
from syssettings import Dictionaries


class DictionaryTest(unittest.TestCase):
    def setUp(self):
        Dictionary.invalidate()

    def tearDown(self):
        Dictionary.invalidate()

    def test_get_words(self):
        """Tests get_words in Dictionary

        """
        Dictionaries.enabled_dictionaries['shlyapa-game.ru.hard.dict'] = True
        Dictionaries.enabled_dictionaries['lksh.2012.august.ru.dict'] = False

        self.assertEqual(Dictionary.loaded_dictionary, None)
        dictionary = Dictionary.get_dict(None)
        self.assertEquals(dictionary, Dictionary.get_dict(None))
        self.assertEqual(dictionary, Dictionary.get_dict(None))
        self.assertEqual(Dictionary.loaded_dictionary, dictionary)
        Dictionary.invalidate()
        self.assertEqual(Dictionary.loaded_dictionary, None)
        dictionary = Dictionary.get_dict(None)
        self.assertTrue(Dictionary.loaded_dictionary is not None)

        count = 4688
        dicts = dictionary.parse_dictionaries_metadata()
        words_cnt = 0
        k = 0
        for cur_dict in dicts:
            self.assertEqual(type(cur_dict),
                             type(DictionaryInfo('', '', 0, True)))
            if cur_dict.enable_by_default:
                k += 1
                words_cnt += cur_dict.words_count

        list_of_words = dictionary.get_words(count, ['123', '456'])
        self.assertEqual(len(list_of_words), min(count, words_cnt))
        self.assertEqual(k, 1)

        for word in list_of_words:
            self.assertTrue(type(word) == unicode)
        self.assertTrue(len(set(list_of_words)) == len(list_of_words))
        self.assertTrue(
            set(list_of_words) <=
            set([word.text for word in dictionary.dictionary_words]))

if __name__ == "__main__":
    unittest.main()
